package com.ruoyi.project.mahjong.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.mahjong.domain.TTablenumber;
import com.ruoyi.project.mahjong.domain.enu.TablenumberState;
import com.ruoyi.project.mahjong.service.ITTablenumberService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.TMember;
import com.ruoyi.project.mahjong.service.ITMemberService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 会员管理Controller
 *
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/mahjong/member")
public class TMemberController extends BaseController
{
    @Autowired
    private ITMemberService tMemberService;
    @Autowired
    private ITTablenumberService tTablenumberService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询会员管理列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMember tMember)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tMember.setBelongShop(shopId.toString());
        startPage();
        List<TMember> list = tMemberService.selectTMemberList(tMember);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('mahjong:member:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(TMember tMember)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tMember.setBelongShop(shopId.toString());
        List<TMember> list = tMemberService.selectTMemberList(tMember);
        return AjaxResult.success(list);
    }
    @PreAuthorize("@ss.hasPermi('mahjong:member:recommendlist')")
    @GetMapping("/recommendlist")
    public TableDataInfo recommend(TMember tMember)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());

            tMember.setBelongShop(shopId.toString());

        List<String> times=tMemberService.getRecommend();
        startPage();
        List<TMember> list = tMemberService.recommend(tMember,times);
        return getDataTable(list);
    }
    @GetMapping("/getzilist")
    public AjaxResult list( )
    {
            List<TTablenumber> list = tTablenumberService.selectTTablenumberList(new TTablenumber());
        return AjaxResult.success(list);
    }
    @GetMapping("/getpoolist")
    public AjaxResult getpoolist( )
    {

        List<TTablenumber> list = tTablenumberService.getpoolist();
        return AjaxResult.success(list);
    }
    @GetMapping("/getzilist2")
    public AjaxResult list2( )
    {

        TMember tMember= new TMember();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tMember.setBelongShop(shopId.toString());

        List<TMember> list = tMemberService.selectTMemberList(tMember);
        return AjaxResult.success(list);
    }
    @GetMapping("/getzchooseilist")
    public AjaxResult getzchooseilist( )
    {
        TTablenumber tTablenumber=new TTablenumber();
        tTablenumber.setStatus(TablenumberState.START.getValue());
        List<TTablenumber> list = tTablenumberService.getzchooseilist(tTablenumber);
        return AjaxResult.success(list);
    }
    @PostMapping("/goPollMember")
    public AjaxResult goPollMember(@RequestBody TMember tMember )
    {
       tMemberService.goPollMember(tMember);
        return AjaxResult.success();
    }
    /**
     * 导出会员管理列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:member:export')")
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMember tMember)
    {
        List<TMember> list = tMemberService.selectTMemberList(tMember);
        ExcelUtil<TMember> util = new ExcelUtil<TMember>(TMember.class);
        return util.exportExcel(list, "member");
    }

    /**
     * 获取会员管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tMemberService.selectTMemberById(id));
    }

    /**
     * 新增会员管理
     */
    @PreAuthorize("@ss.hasPermi('mahjong:member:add')")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMember tMember)
    {
        System.out.println(tMember);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tMember.setBelongShop(shopId.toString());
        return toAjax(tMemberService.insertTMember(tMember));
    }

    /**
     * 修改会员管理
     */
    @PreAuthorize("@ss.hasPermi('mahjong:member:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMember tMember)
    {
        return toAjax(tMemberService.updateTMember(tMember));
    }

    /**
     * 删除会员管理
     */
    @PreAuthorize("@ss.hasPermi('mahjong:member:remove')")
    @Log(title = "会员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            Integer integer = tMemberService.updateTMemberById(id);
        }
        return  AjaxResult.success("删除成功");
    }

    @GetMapping("rollbackMember/{ids}")
    public AjaxResult rollbackMember(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            Integer integer = tMemberService.rollbackMember(id);
        }
        return  AjaxResult.success("恢复成功");
    }
}
